$(document).ready(function() {
	updateListener();
	updateEmailListener();
	updateRoleListener();
	searchListener();
	alertCloseListener();
	ajaxListener();
});

function updateListener(){
	$("#updateButton").click(function(event) {
		event.preventDefault();
		var valid = bsValidateLength($("#expedioQuoteId"), 6);
		if (valid) {
			valid = bsValidateLength($("#email"), 8);
		}
		if (valid) {
			$.ajax({
				type : "POST",
				url : "utils/Update",
				data : $("#updateForm,#envForm").serialize(),
				success : function(list) {
					updateSuccess(list);
				}
			});
		}
	});
}

function updateEmailListener(){
	$("#updateEmailLink").click(function(event) {
		event.preventDefault();
		var valid = bsValidateLength($("#expedioQuoteId"), 6);
		if (valid) {
			valid = bsValidateLength($("#email"), 8);
		}
		if (valid) {
			$.ajax({
				type : "POST",
				url : "utils/UpdateEmail",
				data : $("#updateForm,#envForm").serialize(),
				success : function(list) {
					updateSuccess(list);
				}
			});
		}
	});
}

function updateRoleListener(){
	$("#updateRoleLink").click(function(event) {
		event.preventDefault();
		var valid = bsValidateLength($("#expedioQuoteId"), 6);
		if (valid) {
			$.ajax({
				type : "POST",
				url : "utils/UpdateRole",
				data : $("#updateForm,#envForm").serialize(),
				success : function(list) {
					updateSuccess(list);
				}
			});
		}
	});
}

function updateSuccess(list){
	if(list.length > 0){
		for(var i=0;i<list.length;i++){
			var o = list[i];
			var tr = createTr([i+1,o.guid,o.expRefId,o.salesChannel,o.customerName,o.salesChannelType,o.firstName,o.lastName,o.userEmail,o.userRole]);
			$(tr).appendTo("#urlTable tbody");
		}
		$("#successText").html("<strong>Success!</strong> Url Parameters updated successfully.");
		$("#successDiv").slideDown();
		$("#urlTable").slideDown();
	}else {
		$("#errorText").html("<strong>Snap!</strong> Search params return no records. Please check whether the ExpQuoteId and env are correct.");
		$("#failureDiv").slideDown();
	}
}

function searchListener(){
	$("#searchExpId").click(function(event) {
		event.preventDefault();
		var valid = bsValidateLength($("#expedioQuoteId"), 6);
		if (valid) {
			$.ajax({
				type : "POST",
				url : "utils/searchExpId",
				data : $("#updateForm,#envForm").serialize(),
				success : function(list) {
					if(list.length > 0){
						for(var i=0;i<list.length;i++){
							var o = list[i];
							var tr = createTr([i+1,o.guid,o.expRefId,o.salesChannel,o.customerName,o.salesChannelType,o.firstName,o.lastName,o.userEmail,o.userRole]);
							$(tr).appendTo("#urlTable tbody");
						}
						$("#urlTable").slideDown();
					}else {
						$("#errorText").html("<strong>Snap!</strong> Search params return no records. Please check whether the ExpQuoteId and env are correct.");
						$("#failureDiv").slideDown();
					}
				}
			});
		}
	});
}

function createTr(values){
	return encloseTds(createTd(values));
}

function createTd(values){
	var tds = "";
	for(var i=0;i<values.length;i++){
		tds=tds+"<td>"+values[i]+"</td>";
	}
	return tds;
}

function encloseTds(value){
	return "<tr>"+value+"</tr>";
}

function ajaxListener(){
	$(document).ajaxSend(function(event, request, settings) {
		$("#successDiv").hide();
		$("#failureDiv").hide();
		$("#urlTable tbody").html("");
		$("#urlTable").hide();
		$('#overlay').show();
		$('#loading-indicator').slideDown();
	});
	$(document).ajaxComplete(function(event, request, settings) {
		$('#overlay').hide();
		$('#loading-indicator').hide();
	});
}

function alertCloseListener(){
	$(".close").click(function(event) {
		event.preventDefault();
		$(this).parent().hide();
	});
}

function bsValidateLength(element, minLength) {
	element.closest("div .control-group").removeClass("error");
	element.parent().find(".help-inline").remove();
	if (element.val().length < minLength) {
		element.closest("div .control-group").addClass('error');
		element.parent().append(
				"<span class='help-inline'>Atleast " + minLength
						+ " characters expected!</span>");
		return false;
	} else {
		return true;
	}
}